#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
unsigned int f_sum(unsigned int arr[][10], unsigned int  arr_1[][10],int i, int j, int dem);

class Matrix
{
public:
    Matrix();
	Matrix(int n); //n为维度
	~Matrix();
	void f_mult(int times);
	void f_display();
	void f_setmatrix();
	void f_setMatrix(int n);

private:
	int m_Dem;
	unsigned int m_Data[10][10];
};

Matrix::Matrix()
{
}

Matrix::Matrix(int n)
{
	m_Dem = n;
}

Matrix::~Matrix()
{
}

void Matrix::f_mult(int times)
{
	unsigned int m_data_cpy[10][10];
	unsigned int m_data_cpy_1[10][10];
	for (size_t i = 0; i < 10; i++)
	{
		for (size_t j = 0; j < 10; j++)
		{
			m_data_cpy[i][j] = m_Data[i][j];
		}
	}
	/*Cij = E_ai*aj*/
	/*再研究表示方法*/
	for (size_t w = 0; w < times-1; w++)
	{
		for (size_t i = 0; i < 10; i++)
		{
			for (size_t j = 0; j < 10; j++)
			{
				m_data_cpy_1[i][j] = m_Data[i][j];
			}
		}
		for (size_t i = 0; i < m_Dem; i++)
		{
			for (size_t j = 0; j < m_Dem; j++) //现在这个函数只能求二次幂
			{
				//对Cij求和的函数
				m_Data[i][j] = f_sum(m_data_cpy_1, m_data_cpy, i, j, m_Dem); //他会对原来的东西进行修改,所以要创建一份副本 
			}
		}
	}
}

void Matrix::f_display()
{
	for (size_t i = 0; i < m_Dem; i++)
	{
		for (size_t j = 0; j < m_Dem; j++)
		{
			if (j == m_Dem - 1)
			{
				cout << m_Data[i][j];
			}
			else cout << m_Data[i][j] << " ";
		}
		cout << endl;
	}
}

void Matrix::f_setmatrix()
{
	for (size_t i = 0; i < m_Dem; i++)
	{
		for (size_t j = 0; j < m_Dem; j++)
		{
			cin >> m_Data[i][j];
		}
	}
}

void Matrix::f_setMatrix(int n)
{
	m_Dem = n;
}








int main()
{
	int T;
	cin >> T;
	Matrix *matrixOut = new Matrix[T];
	for (size_t i = 0; i < T; i++)
	{
		int Dem, k; //k times;
		cin >> Dem;
		cin >> k;
		matrixOut[i].f_setMatrix(Dem);
		matrixOut[i].f_setmatrix();
		matrixOut[i].f_mult(k);
	}
	for (size_t i = 0; i < T; i++)
	{
		matrixOut[i].f_display();
	}
	delete[]matrixOut;
	system("pause");
}



unsigned int f_sum(unsigned int  arr[][10], unsigned int  arr_1[][10], int i, int j,int dem)//矩阵乘法sum模块
{
	int temp = 0;
	int s_i = 0, s_j = 0; //I_line j_down
	for (size_t s = 0; s < dem; s++)
	{
		temp += arr[i][s_j] * arr_1[s_i][j]; //请求和
		s_j++;
		s_i++;

	}
	return temp;
}
